/**
 ******************************* ICE *******************************
 * ========================================================================
 * Project:		iceservices
 * Package:		org.ice4apps.service
 * File:		IceEnterpriseService.java
 * Author: 		robin
 * Date: 		May 3, 2010
 * Description:	
 * ========================================================================
 * 
 */

package org.ice4apps.service;

import java.util.List;
import java.util.UUID;
import javax.ejb.Stateless;
import org.ice4apps.service.bean.LoginServiceRequest;
import org.ice4apps.service.bean.OrganizationAddServiceRequest;
import org.ice4apps.service.bean.OrganizationGetServiceResponse;
import org.ice4apps.service.bean.OrganizationListEntry;
import org.ice4apps.service.bean.OrganizationListGetServiceResponse;
import org.ice4apps.master.dao.Enterprise;
import org.ice4apps.master.dao.Organization;
import org.ice4apps.master.dataservices.IceMasterDataServiceLocator;
import org.ice4apps.talk.dao.TalkEnterprise;
import org.ice4apps.talk.dao.TalkOrganization;
import org.ice4apps.talk.dataservices.IceTalkDataServiceLocator;

@Stateless
public class IceEnterpriseService extends IceOrganizationService implements IceEnterpriseServiceLocal {

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceEnterpriseServiceLocal#addOrganization(java.lang.String, org.ice4apps.service.bean.OrganizationAddServiceRequest)
     */
    public void addOrganization (String sessionId,
                                 OrganizationAddServiceRequest req) {
        try {
            IceMasterDataServiceLocator.getIceMasterDataService ().getSession (sessionId);
        } catch (Exception ex1) {
        }
        try {
            req.validate ();
            String id = UUID.randomUUID ().toString ();
            Organization org = new Organization ();
            org.setId (id);
            org.setName (req.getName ());
            Enterprise ent = IceMasterDataServiceLocator.getIceMasterDataService ().getEnterpriseByName (req.getEnterpriseName ());
            org.setEnterprise (ent);
            org.setFeatures (Long.MAX_VALUE);
            Long creationDate = System.currentTimeMillis ();
            org.setCreationdate (creationDate);
            IceMasterDataServiceLocator.getIceMasterDataService ().addOrganization (org);
            TalkOrganization talkOrg = new TalkOrganization ();
            talkOrg.setCreationdate (creationDate);
            talkOrg.setId (id);
            talkOrg.setExtensionLength (req.getExtensionLength ());
            talkOrg.setFeatures (Long.MAX_VALUE);            
            TalkEnterprise talkEnt = IceTalkDataServiceLocator.getIceTalkDataService ().getEnterprise (ent.getId ());
            talkOrg.setTalkEnterprise (talkEnt);
            IceTalkDataServiceLocator.getIceTalkDataService ().addOrganization (talkOrg);         
        } catch (Exception ex) {
            ex.printStackTrace ();
        }
        
    }

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceEnterpriseServiceLocal#login(org.ice4apps.service.bean.LoginServiceRequest)
     */
    public String login (LoginServiceRequest req) {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceEnterpriseServiceLocal#logout(java.lang.String)
     */
    public void logout (String sessionId) {
        // TODO Auto-generated method stub
        
    }

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceEnterpriseServiceLocal#getOrganization(java.lang.String, java.lang.String)
     */
    public OrganizationGetServiceResponse getOrganization (String sessionId,
                                                           String name) {
        try {
            IceMasterDataServiceLocator.getIceMasterDataService ().getSession (sessionId);
            Organization org = IceMasterDataServiceLocator.getIceMasterDataService ().getOrganizationByName (name);
            TalkOrganization talkOrg = IceTalkDataServiceLocator.getIceTalkDataService ().getOrganization (org.getId ());
            OrganizationGetServiceResponse resp = new OrganizationGetServiceResponse ();
            resp.setName (org.getName ());
            resp.setEnterpriseName (org.getEnterprise ().getName ());
            resp.setExtensionLength (talkOrg.getExtensionLength ());
            return resp;
        } catch (Exception ex1) {
            ex1.printStackTrace ();
        }
        
        return null;
    }

    /* (non-Javadoc)
     * @see org.ice4apps.service.IceEnterpriseServiceLocal#getOrganizationList(java.lang.String, java.lang.String)
     */
    public OrganizationListGetServiceResponse getOrganizationList (String sessionId,
                                                                   String enterpriseName) {
        try {
            IceMasterDataServiceLocator.getIceMasterDataService ().getSession (sessionId);
            List <Organization> orgs = IceMasterDataServiceLocator.getIceMasterDataService ().getOrganizationListByEnterprise (enterpriseName);
            OrganizationListGetServiceResponse resp = new OrganizationListGetServiceResponse ();
            for (Organization org : orgs) {
                OrganizationListEntry entry = new OrganizationListEntry ();
                entry.setEnterpriseName (org.getEnterprise ().getName ());
                entry.setName (org.getName ());
                resp.add (entry);
            }
            return resp;
        } catch (Exception ex1) {
            ex1.printStackTrace ();
        }
        return null;
    }
    
}
